package com.lzz.bottomencapsulation.popupwindow;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.lzz.bottomencapsulation.R;


/***
 @package ${package}
 @filename ${居中显示dialog}
 */
public abstract class CenterBaseDialog  extends Dialog {
    protected View.OnClickListener onClickListener;
    protected Context context;
    private int gravity=Gravity.CENTER;
    private int mWidth=WindowManager.LayoutParams.MATCH_PARENT;
    boolean siInit=false;
    boolean isBack=false;//是否屏蔽返回键
    public CenterBaseDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
        this.context=context;
        setCanceledOnTouchOutside(true);//设置点击Dialog以外的地方不能让Dialog消失
    }

    public CenterBaseDialog(@NonNull Context context,int gravity) {
        super(context, R.style.Dialog);
        this.context=context;
        this.gravity=gravity;
        setCanceledOnTouchOutside(true);//设置点击Dialog以外的地方不能让Dialog消失
    }

    public CenterBaseDialog(@NonNull Context context,boolean isBack) {
        super(context, R.style.Dialog);
        this.context=context;
        setCanceledOnTouchOutside(true);//设置点击Dialog以外的地方不能让Dialog消失
        this.isBack=isBack;
    }

    public CenterBaseDialog(@NonNull Context context,int gravity, int width) {
        super(context, R.style.Dialog);
        this.context=context;
        this.gravity=gravity;
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        this.mWidth=(int)(displayMetrics.widthPixels - width);;
        setCanceledOnTouchOutside(true);//设置点击Dialog以外的地方不能让Dialog消失
    }
    protected Window window;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context,getView(), null);
        window= this.getWindow();
        window.setContentView(view);
        window.setGravity(gravity);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = mWidth;
        window.setAttributes(params);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//防止遮挡虚拟键盘
        if (!siInit){
            initView();
            siInit=true;
        }
    }

    protected abstract int getView();//设置视图

    protected abstract void initView();//初始化

    public Dialog setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!siInit){
        initView();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        siInit=false;
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (!isBack)return super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 屏蔽返回键
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
